package bmcsoft.webservicelearning;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.io.*;

public class WebBrowser extends JApplet {
    //JEditor pane to view HTML files
    private JEditorPane jep = new JEditorPane();

    private JScrollPane jspViewer = new JScrollPane();

    //Label for URL
    private JLabel jlblurl = new JLabel("URL");

    //Text field for entering URL
    private JTextField jtfURL = new JTextField();

    /** Initialize the applet*/
    @Override
    public void init() {
        //Create a panel jpURL to hold the label and text field
        JPanel jpURL = new JPanel();
        jpURL.setLayout(new BorderLayout());
        jpURL.add(jlblurl,BorderLayout.WEST);
        jpURL.add(jtfURL,BorderLayout.CENTER);

        //Place jpURL and jspViewer in the applet
        add(new JScrollPane(jspViewer),BorderLayout.CENTER);
        add(jpURL,BorderLayout.NORTH);

        //Set jep noneditable
        jep.setEditable(false);

        //Register listener
        jep.addHyperlinkListener(new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                try {
                    jep.setPage(e.getURL());
                }catch (IOException ex){
                    System.out.println(ex);
                }
            }
        });
        jtfURL.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //Get the URL from text field
                    URL url = new URL(jtfURL.getText().trim());

                    //Display the HTML file
                    jep.setPage(url);
                }catch (IOException ex){
                    System.out.println(ex);
                }
            }
        });
    }
}
